package com.pxst.annotations.validated;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ValidationException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 两个必须同时有值，并且第一个属性值不大于第二个属性值的处理逻辑
 * @author rj
 * @create 2023-09-17-23:31
 */
@Slf4j
public class DateValidated implements ConstraintValidator<DateValidator, Map<String,Object>> {

    @Override
    public boolean isValid(Map<String, Object> map, ConstraintValidatorContext constraintValidatorContext) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

        HashMap<String, List<List<String>>> stringListHashMap = new HashMap<>();

        // 添加需要校验的类名和属性名。每次添加一个类名，添加对应的属性名（时间：起始时间，结束时间）
        // key: className value: List<List<String>>
        stringListHashMap.put("FiOrderReq",
                Arrays.asList(
                    Arrays.asList("startDate","endDate"),
                    Arrays.asList("updatedStartDate","updatedEndDate"),
                    Arrays.asList("completedStartDate","completedEndDate")
                )
        );

        stringListHashMap.put("MerchantStatementReq",
                Arrays.asList(
                        Arrays.asList("transactionStartDate","transactionEndDate")
                )
        );

        stringListHashMap.put("TpiDownloadReq",
                Arrays.asList(
                        Arrays.asList("startTime","endTime")
                )
        );

        stringListHashMap.put("NonMerchantStatementReq",
                Arrays.asList(
                        Arrays.asList("transactionStartDate","transactionEndDate"),
                        Arrays.asList("completedStartDate","completedEndDate")
                )
        );

        // 全部类名
        Set<String> strings = stringListHashMap.keySet();

        for (String string : strings) {
            if (!map.get("class").equals(string)){
                continue;
            }

            for (List<String> list : stringListHashMap.get(string)) {
                if ((map.get(list.get(0)) != null && map.get(list.get(1)) == null) || (map.get(list.get(0)) == null && map.get(list.get(1)) != null)) {
                    throw new ValidationException("起始时间和结束时间必须同时输入");
                } else if (map.get(list.get(0)) != null && map.get(list.get(1)) != null) {
                    try {
                        boolean isBoolean = dateFormat.parse(map.get(list.get(0)).toString()).before(dateFormat.parse(map.get(list.get(1)).toString()));
                        if (!isBoolean){
                            return isBoolean;
                        }
                    } catch (ParseException e) {
                        log.error("Date格式转换异常");
                        e.printStackTrace();
                    }
                }
            }
            return true;
        }

        return true;
    }

}
